﻿using System;
using System.Linq;
using System.Web.UI.WebControls;

namespace RaceList.Admin.Relay
{
    public partial class AwardAdd : System.Web.UI.Page
    {
        ipowdb_suibian_webDataContext db = ConnHelper.ipowdb_suibian_web();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBind();
            }
        }
        protected void DataBind()
        {
            //查找未删除的活动
            var cycles = db.Activity_Cycle.Where(s => s.isDel == false).Select(s => new { s.Id, s.title });
            foreach (var item in cycles)
            {
                ddlActivity.Items.Add(new ListItem(item.title, item.Id.ToString()));
            }
            var sponsors = db.Activity_Sponsor.Where(s => s.isDel == false).Select(s => new { s.Id, s.Name });
            foreach (var item in sponsors)
            {
                ddlSponsor.Items.Add(new ListItem(item.Name, item.Id.ToString()));
            }
            ddlSponsor.Items.Insert(0, new ListItem("无", "null"));
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Activity_Awards award = new Activity_Awards
            {
                addTime = DateTime.Now,
                cycleId = Convert.ToInt32((string) ddlActivity.SelectedValue),
                dayCount = Convert.ToInt32((string) txtDay_Count.Text.Trim()),
                editTime = DateTime.Now,
                name = txtTitle.Text.Trim(),
                remark = txtRemark.Text.Trim(),
                sponsorId = CommonHelp.GetSponsor(ddlSponsor.SelectedValue),
                status = CommonHelp.GetStatus(ddlStatus.SelectedIndex),
                sumCount = Convert.ToInt32((string) txtCount.Text.Trim()),
            };


            try
            {
                db.Activity_Awards.InsertOnSubmit(award);
                db.SubmitChanges();
                //.AddOperatingLog("奖品", "添加", "添加了奖品 ID:" + award.Id + ",Title:" + award.name);
                StringHelper.Alert(this, "奖品发布成功！", "AwardList.aspx");
            }
            catch (Exception ex)
            {
                //.AddErrorLog(ex);
                this.Message.Text = "发布失败! 失败原因：" + ex.Message + "。详细错误信息请查看错误日志。";
            }
        }
    }
}